<?php

namespace app\models;

use Yii;
use dektrium\user\models\User;

/**
 * This is the model class for table "logs".
 *
 * @property integer $id
 * @property integer $code
 * @property integer $type
 * @property integer $oper_type
 * @property string $desc
 * @property string $params
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class Logs extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'logs';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['code', 'type', 'oper_type', 'user_id'], 'integer'],
            [['created_at'], 'safe'],
            [['desc', 'params'], 'string', 'max' => 128],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'code' => Yii::t('app', 'Code'),
            'type' => Yii::t('app', 'Type'),
            'oper_type' => Yii::t('app', 'Oper Type'),
            'desc' => Yii::t('app', 'Desc'),
            'params' => Yii::t('app', 'Params'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            //'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }

	// 保存前处理 
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            //$this->user_id    = isset(\Yii::$app->user->id) ? \Yii::$app->user->id : 93; // 系统账号
			if(isset(Yii::$app->user)){
				$this->user_id    = Yii::$app->user->id;
			}else{
				$this->user_id    = 93;
			}

			if(empty($this->type)){
				$this->type = 1;
			}
        }
        return parent::beforeSave($insert);
    }//}}}



    // 管理员 
    public function getMember(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

}
